<?php

namespace App\Http\Controllers\Admin\Census;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use App\Services\Tools\ExportService;
use App\Services\Tools\HelperService;
use Carbon\Carbon;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;
use App\Models\MainDB\GiveGiftDay;
use Illuminate\Support\Facades\Redis;

class RoomAccountDataController extends BaseController {

    /**
     * @api {get} /census/room-account-data 所有房间流水
     * @apiVersion 0.1.0
     * @apiName  数据统计-所有房间流水
     * @apiGroup 数据统计
     *
     * @apiParam {string} date_start   开始日期,非必传
     * @apiParam {string} date_end     结束日期，非必传
     * @apiParam {string} keyword      关键字，非必传 传数组时查房间id 字符串时查房间名
     * @apiParam {string} type         等级，必传 1:按天 2：按周 3：按月
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
          "list": [
                {
                    "id": 3023,
                    "number(流水)": "12139990",
                    "show_date(日期)": "2020/11/02 - 2020/11/08",
                    "room_id(房间编号)": 2499308,
                    "room_name(房间名称)": "毛",
                    "user_code(房主编号)": 793781,
                    "date(传递给详情页的参数)":  "2020-11-05"
                }
          "meta": {
               "page（页数）": 1,
               "limit（每页数量）": 100,
              "next（下一页）": null,
              "total（总数）": 2
          }
    }
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'date_start' => 'filled|string|date_format:Y-m-d',
                'date_end' => 'filled|string|date_format:Y-m-d',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
                'keyword' => 'filled|string',
                "type" => 'required|numeric|in:1,2,3',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_room_account')->getQueryList($request->input());

        return Response::sendList($list);
    }

    /**
     * @api {get} /census/room-account-data/detail 所有房间流水-详情
     * @apiVersion 0.1.0
     * @apiName  数据统计-所有房间流水-详情
     * @apiGroup 数据统计
     *
     * @apiParam {string} date         日期, 必传
     * @apiParam {number} id           id，必传
     * @apiParam {string} keyword      关键字，非必传
     * @apiParam {number} type         等级，必传 1:按天 2：按周 3：按月
     *
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": {
                "list": [
                    {
                        "number": "1333332.00",
                        "date": "2020-11",
                        "id": 5399,
                        "room_id": 6550716,
                        "room_name": "zale",
                        "add_time": "2020-11-04 15:35:14",
                        "user_code": 617040
                    }
                ]
                "meta": {
                    "page": 1,
                    "limit": 10,
                    "next": "http://qipao-admin-api.com/census/room-account-data/detail?=2",
                    "total": 60
                }
            }
        }
     *
     */
    public function detail(Request $request){
        //参数校验
        try {
            $this->validate($request, [
                'date' => 'required|string',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
                "type" => 'required|numeric|in:1,2,3',
                'id' => 'required|numeric',
                'keyword' => 'filled|string',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_room_account')->detail($request->input());

        $result = Response::sendList($list);
        $list = json_decode($result,true);
        if ($list['data']['meta']['total'] > 1000) {
            $list['data']['meta']['total'] = 1000;
        }
        return json_encode($list,true);
    }

    /**
     * @api {get} /census/room-account-data/export/excel 所有房间流水-导出
     * @apiVersion 0.1.0
     * @apiName  数据统计-所有房间流水-导出
     * @apiGroup 数据统计
     *
     * @apiParam {string} date_start   开始日期,非必传
     * @apiParam {string} date_end     结束日期，非必传
     * @apiParam {string} keyword      关键字，非必传
     * @apiParam {string} type         等级，必传 1:按天 2：按周 3：按月
     * @apiSuccessExample 成功响应
        {
        "code": 200,
        "data": {
            "download_url": "asdasdasd.com"
        }
     *
     */
    public function export(Request $request){
        //参数校验
        try {
            $this->validate($request, [
                'date_start' => 'filled|string|date_format:Y-m-d',
                'date_end' => 'filled|string|date_format:Y-m-d',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
                'keyword' => 'filled|string',
                "type" => 'required|numeric|in:1,2,3',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }
        $params = $request->input();
        $params['page'] = 1;
        $params['perPage'] = 10000;
        $list = app('repo_room_account')->getQueryList($params);

        $result = [];
        if ($list){
            foreach ($list as $item) {
                $result[] = [
                    $item->show_date,
                    $item->user_code,
                    $item->room_id,
                    HelperService::wxNickNameFormat($item->room_name),
                    $item->number,
                ];
            }
        }

        $headings = [
            '日期',
            '房主编号',
            '房间编号',
            '房间名称',
            '流水',
        ];
        return app('repo_export')->export(4, $params, $headings, $result);
    }
}
